package com.cloudkinto.controller.fbaproduct;

import com.cloudkinto.base.BaseController;
import com.cloudkinto.common.annotations.UserSessionRequired;
import com.cloudkinto.common.web.PageResult;
import com.cloudkinto.common.web.SingleResult;
import com.cloudkinto.service.fbaproduct.FbaProductService;
import com.cloudkinto.service.fbaproduct.vo.FbaProductAddReq;
import com.cloudkinto.service.fbaproduct.vo.FbaProductUpdateReq;
import com.cloudkinto.service.product.ProductInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author zhangyy
 * @date 2020/8/20 9:02
 * @description:
 */
@RestController
@Api(tags = "fba商品模块模块")
@RequestMapping("/api/fbaProduct")
public class FbaProductController extends BaseController {

    @Autowired
    private FbaProductService service;
    @Autowired
    private ProductInfoService productInfoService;

    @UserSessionRequired
    @GetMapping("/addInit")
    @ApiOperation(value = "新增初始化", notes = "新增初始化")
    public SingleResult addInit() {
        return service.addInit();
    }

    @UserSessionRequired
    @PostMapping("")
    @ApiOperation(value = "新增", notes = "新增")
    public SingleResult add(@RequestBody @Validated FbaProductAddReq req) {
        Long userId = getCurrentUserId();
        Long companyId = getCompanyId();
        return SingleResult.success("新增成功", service.add(req, userId, companyId));
    }

    @UserSessionRequired
    @GetMapping("/updateInit/{id}")
    @ApiOperation(value = "编辑初始化", notes = "编辑初始化")
    public SingleResult updateInit(@PathVariable("id") Long id) {
        return service.updateInit(id);
    }

    @UserSessionRequired
    @PutMapping("")
    @ApiOperation(value = "修改", notes = "修改")
    public SingleResult update(@RequestBody @Validated FbaProductUpdateReq req) {
        Long userId = getCurrentUserId();
        Long companyId = getCompanyId();
        return SingleResult.success("修改成功", service.update(req, userId, companyId));
    }

    @UserSessionRequired
    @DeleteMapping("/{id}")
    @ApiOperation(value = "根据id删除", notes = "根据id删除")
    public SingleResult delete(@PathVariable("id") Long id) {
        Long userId = getCurrentUserId();
        return SingleResult.success("删除成功", service.delete(id, userId));
    }

    @UserSessionRequired
    @PostMapping("deleteBatch")
    @ApiOperation(value = "批量删除", notes = "根据id删除")
    public SingleResult deleteBatch(@RequestBody List<Long> ids) {
        Long userId = getCurrentUserId();
        service.deleteBatch(ids, userId);
        return SingleResult.success();
    }

    @UserSessionRequired
    @GetMapping("/{id}")
    @ApiOperation(value = "根据id查看详情", notes = "根据id查看详情")
    public SingleResult detail(@PathVariable("id") Long id) {
        return SingleResult.success("查询成功", service.detail(id));
    }

    @UserSessionRequired
    @GetMapping("/pageInit")
    @ApiOperation(value = "查询初始化", notes = "查询初始化")
    public SingleResult pageInit() {
        return service.pageInit();
    }

    @UserSessionRequired
    @GetMapping("/listPage")
    @ApiOperation(value = "查询（分页）", notes = "查询（分页）")
    @ApiImplicitParams({@ApiImplicitParam(name = "productType", value = "产品类型"),
            @ApiImplicitParam(name = "current", value = "当前页"),
            @ApiImplicitParam(name = "keyword", value = "关键字（模糊查询）"),
            @ApiImplicitParam(name = "pageSize", value = "页面大小")})
    public PageResult listPage(HttpServletRequest request) {
        Long companyId = getCompanyId();

        Map<String, String> map = new HashMap<>();
        String keyword = request.getParameter("keyword");
        String productType = request.getParameter("productType");
        String current = request.getParameter("current");
        String size = request.getParameter("pageSize");
        if (companyId != null) {
            map.put("companyId", companyId + "");
        }
        map.put("productType", productType);
        map.put("keyword", keyword);
        map.put("current", current);
        map.put("pageSize", size);
        return service.getListPage(map);
    }

    @UserSessionRequired
    @PostMapping("/addProductFile")
    @ApiOperation(value = "批量上传产品信息")
    public SingleResult addProductFile(MultipartFile file) throws FileNotFoundException {
        service.importProductFile(file, getCurrentUserId(), getCompanyId());
        return SingleResult.success();
    }

    @UserSessionRequired
    @PostMapping("/exportExcel")
    @ApiOperation(value = "批量导出")
    public void exportExcel(HttpServletResponse response) throws IOException {
        service.exportExcel(response);
    }

    @UserSessionRequired
    @GetMapping("/productListPage")
    @ApiOperation(value = "一件自发商品（分页）", notes = "查询（分页）")
    @ApiImplicitParams({@ApiImplicitParam(name = "productType", value = "产品类型"),
            @ApiImplicitParam(name = "current", value = "当前页"),
            @ApiImplicitParam(name = "keyword", value = "关键字（模糊查询）"),
            @ApiImplicitParam(name = "pageSize", value = "页面大小")})
    public PageResult productListPage(HttpServletRequest request) {
        Long companyId = getCompanyId();

        Map<String, String> map = new HashMap<>();
        String keyword = request.getParameter("keyword");
        String productType = request.getParameter("productType");
        String current = request.getParameter("current");
        String size = request.getParameter("pageSize");

        map.put("companyId", String.valueOf(companyId));
        map.put("productType", productType);
        map.put("keyword", keyword);
        map.put("current", current);
        map.put("pageSize", size);
        return productInfoService.getListPage(map);
    }

    @UserSessionRequired
    @GetMapping("productDetail/{id}")
    @ApiOperation(value = "根据id一件自发商品详情", notes = "根据id查看详情")
    public SingleResult productDetail(@PathVariable("id") Long id) {
        return SingleResult.success("查询成功", productInfoService.detail(id));
    }

//
//    @UserSessionRequired
//    @GetMapping("/exportProductPdf")
//    @ApiOperation(value = "导出產品标文件", notes = "导出產品标文件")
//    public void exportProductPdf(Long id, int count, HttpServletResponse response) throws IOException {
//        Long userId = getCurrentUserId();
//        service.exportProductCodePdf(id, count, response);
//    }
}
